package com.example.floristshop.adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.example.floristshop.R;
import com.example.floristshop.database.Flower;

import java.util.List;

public class SelectFlowerAdapter extends ArrayAdapter<Flower> {

    private int itemView;

    public SelectFlowerAdapter(@NonNull Context context, int resource, @NonNull List<Flower> objects) {
        super(context, resource, objects);
        itemView = resource;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        Flower flower = getItem(position);
        View view;
        ViewHolder viewHolder;
        if(convertView == null){
            view = LayoutInflater.from(getContext()).inflate(itemView, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.image_iv = (ImageView)view.findViewById(R.id.flower_list_image);
            viewHolder.name_tv = (TextView)view.findViewById(R.id.flower_list_name);
            viewHolder.type_tv = (TextView)view.findViewById(R.id.flower_list_type);
            viewHolder.origin_tv = (TextView)view.findViewById(R.id.flower_list_origin);
            viewHolder.intro_tv = (TextView)view.findViewById(R.id.flower_list_intro);
            view.setTag(viewHolder);
        }else{
            view = convertView;
            viewHolder = (ViewHolder)view.getTag();
        }
        viewHolder.image_iv.setImageResource(flower.getImage());
        viewHolder.name_tv.setText(flower.getName());
        viewHolder.type_tv.setText(flower.getType());
        viewHolder.origin_tv.setText(flower.getOrigin());
        viewHolder.intro_tv.setText(flower.getIntro());

        return view;
    }

    class ViewHolder{
        ImageView image_iv;
        TextView name_tv, type_tv, origin_tv, intro_tv;
    }
}
